<template>
    <!--<h1>计划明细</h1>-->
    <div class="create-plan-page">
        <router-link to="/SparePlan" style="float: right">
            <el-button type="danger" icon="close" size="mini" class="bolder outsideBtnBS"></el-button>
        </router-link>
        <div class="block" style="margin-bottom: 10px">
            <span class="wrapper">
                <el-button type="danger" size="mini" class="bolder outsideBtnBS selectAll" :disabled="saveDisabled" style="margin-left:0!important;" @click="savePlan">保存</el-button>
                <el-button type="danger" size="mini" class="bolder outsideBtnBS selectAll" :disabled="submitDisabled" @click="submitPlan">提交</el-button>
                 <!-- 
                <el-button type="danger" size="mini" disabled class="bolder outsideBtnBS">审核</el-button>
                <el-button type="danger" size="mini" disabled class="bolder outsideBtnBS">审批</el-button>
                -->
            </span>
        </div>

        <el-form :inline="true" :model="createPlan" ref="createPlan" style="border-bottom:1px solid #ccc;">
            <el-form-item label="计划编号" :label-width="labelWidth">
                <el-input size="mini" disabled placeholder="" v-model="createPlan.planCode"></el-input>
            </el-form-item>
            <el-form-item label="制定人" :label-width="labelWidth">
                <el-input size="mini" disabled placeholder="" v-model="createPlan.creatorName"></el-input>
            </el-form-item>
            <el-form-item label="制定时间" :label-width="labelWidth">
                <el-input size="mini" disabled placeholder="" v-model="createPlan.createTime"></el-input>
            </el-form-item>

            <el-form-item label="法人名称" prop="legalPersonName"  :rules="[{ required: true, message: '法人名称不能为空'}]" :label-width="labelWidth">
                <el-select size="mini" v-if="legalPersonLists.length> 1" v-model="createPlan.legalPersonName"  @change="selectLegalPersonName" placeholder="请选择">
                    <el-option v-for="(legalPerson,index) in legalPersonLists" :key="index" :label="legalPerson.legalPersonName" :value="legalPerson.legalPersonName">
                    </el-option>
                </el-select>
                <el-input size="mini" disabled v-else v-model="createPlan.legalPersonName" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="计划来源" prop="source" :rules="[{ required: true, message: '计划来源不能为空'}]" :label-width="labelWidth">
                <el-select size="mini" v-model="createPlan.source" placeholder="请选择">
                    <el-option v-for="(source,index) in sourceLists" :key="index" :label="source.label" :value="source.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="计划币别" prop="currencyCode" :rules="[{ required: true, message: '计划币别不能为空'}]" :label-width="labelWidth">
                <el-select size="mini" v-model="createPlan.currencyCode" placeholder="请选择" @change="selectCurrencyCode">
                    <el-option v-for="(currencyCode,index) in currencyCodeLists" :key="index" :label="currencyCode.label" :value="currencyCode.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="计划总件数" :label-width="labelWidth">
                <el-input size="mini" disabled placeholder="" v-model="createPlan.totalCount"></el-input>
            </el-form-item>
            <el-form-item label="计划总金额" :label-width="labelWidth">
                <el-input size="mini" disabled placeholder="" v-model="createPlan.total"></el-input>
            </el-form-item>
        </el-form>
        <!--<el-form :inline="true" :model="createPlanForm">
            <el-form-item label="备件编码">
                <el-input placeholder="输入备件编码" size="mini" v-model="createPlanForm.materialCode"></el-input>
            </el-form-item>
            <el-form-item label="备件分类">
                <el-select v-model="createPlanForm.materialSubclass" placeholder="请选择" clearable>
                    <el-option v-for="materialSubclass in materialSubclassLists" :label="materialSubclass" :value="materialSubclass">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="danger" size="mini" class="bolder outsideBtnBS">查询</el-button>
                
                <el-button type="danger" size="mini" class="bolder outsideBtnBS" @click="createPlanDetais(sparePageIndex,sparePageSize)">新增</el-button>
            </el-form-item>
        </el-form>
        -->
        <label class="default-label">计划明细：</label>
        <el-button class="bolder outsideBtnBS" size="mini" type="danger"  @click="createPlanDetais(sparePageIndex,sparePageSize)">新增</el-button>
        <!--表格-->
        <el-table :data="planDetailsData" border style="width: 100%;">
            <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
            <el-table-column label="操作"  width="60" align="center">
                <template scope="scope">
                    <div class="operationItem bolder" @click="handleDelete(scope.$index,scope.row)">删除</div>
                    <!-- planDetailsData -->
                </template>
            </el-table-column>
            <el-table-column prop="materialSubclass" label="分类"  header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="materialCode" label="备件编码"   header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="materialName" label="备件名称"  header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="standard" label="规格"  width="60" header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="unit" label="计量单位"  width="80" header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="quantity" label="计划数量"  width="80" header-align="center">
                 <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.quantity" placeholder="请输入" @change="planQuantity(scope.$index, scope.row,$event)"></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="currencyCodeWord" label="币别"  width="60" header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="price" label="单价" header-align="center" align="right">
                 <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.price" placeholder="请输入" @change="planPrice(scope.$index, scope.row,$event)"></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="amount" label="金额"  width="80" header-align="center" align="right"> </el-table-column>
            <el-table-column prop="deliveryTimeExpectation" label="期望到货时间"  header-align="center" width="200">
                <template scope="scope">
                    <el-date-picker type="date" size="mini" :editable="false" v-model="scope.row.deliveryTimeExpectation" placeholder="请选择期望到货时间" :picker-options="pickerOptions" @change="selectedDeliveryTimeExpectation(scope.$index,scope.row)"></el-date-picker>
                </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注"  header-align="center" show-overflow-tooltip>
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.remark" placeholder="请输入"></el-input>
                </template>
            </el-table-column>
        </el-table>        
        <!--新增弹窗-->
        <el-dialog title="选择备件" :visible.sync="dialogFormVisible" :lock-scroll="false">
            <el-form :model="chooseSpare" :inline="true">
                <el-form-item label="备件编号" >
                    <el-input size="mini" v-model="chooseSpare.code" auto-complete="off" placeholder="请输入备件编号"></el-input>
                </el-form-item>
                <el-form-item label="备件名称" >
                    <el-input size="mini" v-model="chooseSpare.name" auto-complete="off" placeholder="请输入备件名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="danger" class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" @click="createPlanDetais(sparePageIndex,sparePageSize)">查询</el-button>
                    <el-button type="danger" class="bolder outsideBtnBS" size="mini" @click="insertMateraialSupplier">确定</el-button>
                </el-form-item>
            </el-form>
            <el-table :data="spareListDate" border style="margin-top:16px;width: 100%;" v-loading.body="loading" element-loading-text="拼命加载中">
                <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
                <el-table-column label="选择" header-align="center" align="center" width="40">
                    <template scope="scope">
                        <el-checkbox size="mini" v-model="scope.row.checked" @change="selectSpare(scope.$index, scope.row)"></el-checkbox>
                    </template>
                </el-table-column>
                <el-table-column prop="code" label="备件编号" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column prop="name" label="编码名称" header-align="center" show-overflow-tooltip></el-table-column>
            </el-table>
            <!--分页-->
            <el-pagination v-if="isPaging" @size-change="handleSizeChange" @current-change="handleCurrentChange"  :current-page.sync="currentPage3" :current-page="sparePageIndex"   :page-sizes="[5, 10, 15 , 20, 25]" :page-size="sparePageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>
        
        </el-dialog>

    </div>
</template>

<script>
    export default {
        data(){
            return{ 
                pickerOptions: {     //期望到货时间不得早于当天
                    disabledDate(time) {
                        return time.getTime() < Date.now() - 8.64e7;
                    }
                },
                labelWidth:"80px",
                createPlan:{
                    planCode:'',
                    legalPersonName:'',
                    legalPersonId:'',
                    creatorName:'',
                    createTime:'',
                    totalCount:'',
                    total:'',
                    source:'',
                    currencyCode:'',
                    currencyCodeWord:''
                },
                legalPersonLists:[],   //法人名称下拉列表中的值
                sourceLists:[],   //计划来源下拉列表的值
                currencyCodeLists:[],   //计划币别下拉列表的值
                materialSubclassLists:[],   //计划明细 查询条件 备件状态
                planItems:[],     //新建的计划明细
                createPlanForm:{
                    spareCode:'',
                    materialSubclass:''
                },
                chooseSpare:{
                    code:'',
                    name:''
                },
                dialogFormVisible: false,
                isPaging:false,
                loading:false,
                sparePageIndex: 1,    //当前页（必传）
                sparePageSize: 10,   //每页多少条
                totalNumber: 20,
                totalPage: 2,
                currentPage3: 3,
                planDetailsData:[],
                planCode:"",   //新增的计划，点击保存之后，可以取到planCode
                planItemId:"",    // 新增的计划，点击保存之后，可以取到planItemId
                seedTableData:[],
                // delFlag:false,     // 没点删除按钮

                spareListDate:[],
                selectedSpareList:[],     // 选中的备件
                selSpare:[],//备件已选
                //查询禁用
                selectDisabled:false,
                saveDisabled:false,  // 保存禁用
                submitDisabled:false  // 提交禁用
            }
        },
        mounted:function(){
            var _this=this;
            _this.$store.commit('currentActive', 'CreatePlan');
            // //ajax前后查询按钮禁用(保存、提交)
            // $(".selectAll").ajaxStart(function(){
            //     _this.selectDisabled=true;
            //     _this.saveDisabled=true;
            //     _this.submitDisabled=true;
            // });
            // $(".selectAll").ajaxStop(function(){
            //     _this.selectDisabled=false;
            //     _this.saveDisabled=false;
            //     _this.submitDisabled=false;
            // });
            _this.planSCDropDownValue();   //计划明细  计划来源和计划明细 下拉列表
            _this.legalPersonDropDownValue(); //计划明细  法人名称 下拉列表
            _this.getUser();
        },
        methods:{
            //获得指定人姓名 createPlan.creatorName
            getUser(){  //法人名称下拉列表值
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"/plan/getUser",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.createPlan.creatorName = data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },

             // 查询条件 法人名称 下拉列表的值
            legalPersonDropDownValue(){  //法人名称下拉列表值
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"legalPerson/ofCurrentUser",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.legalPersonLists = data.data;
                            if(_this.legalPersonLists.length == 1){
                                _this.createPlan.legalPersonId =_this.legalPersonLists[0].id;
                                _this.createPlan.legalPersonName =_this.legalPersonLists[0].legalPersonName
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            // 下拉列表选择法人名称
            selectLegalPersonName(){
                var _this=this;
                for(var i=0;i<_this.legalPersonLists.length;i++){
                    if(_this.createPlan.legalPersonName==_this.legalPersonLists[i].legalPersonName){
                        _this.createPlan.legalPersonId=_this.legalPersonLists[i].id;
                    }
                }
            },
            //  计划明细  计划来源和计划明细 下拉列表
            planSCDropDownValue(){
                var _this=this;
                var tmp = ["source","currencyCode"];
                $.ajax({
                    type:'POST',
                    url:_this.procurement_service+'procurement/getPullDownList',
                    cache:false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success:function(data){
                        if(data.success){
                            _this.sourceLists = data.data.source;
                            _this.currencyCodeLists = data.data.currencyCode;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            // 下拉列表选择币别
            selectCurrencyCode(){
                var _this=this;   
                // 表格中的币别显示汉字
                for(var i=0;i<_this.currencyCodeLists.length;i++){
                    if(_this.createPlan.currencyCode == _this.currencyCodeLists[i].value){
                        _this.createPlan.currencyCodeWord = _this.currencyCodeLists[i].label;
                    }
                }
                for(var i=0;i<_this.planDetailsData.length;i++){
                    _this.planDetailsData[i].currencyCode = _this.createPlan.currencyCode;
                    _this.planDetailsData[i].currencyCodeWord = _this.createPlan.currencyCodeWord;
                }
            },
            // 新增按钮   新增计划明细------打开选择备件的弹窗
            // 选择备件 /material/search 
            createPlanDetais(sparePageIndex,sparePageSize){
                var _this=this;
                _this.dialogFormVisible = true;
                _this.loading=true;
                _this.spareListDate=[];   //备件列表为空
                var tmp={code:_this.chooseSpare.code,name:_this.chooseSpare.name,pageIndex:sparePageIndex,pageSize :sparePageSize};
                _this.selectDisabled=true;
                $.ajax({
                    type: 'POST',
                    url: _this.metadata_service+"material/searchEnabled",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.loading=false;
                            _this.isPaging=true; 
                            _this.spareListDate=data.data.materialList; 
                            _this.totalNumber=data.data.totalNumber;    //总条数
                            _this.totalPage=data.totalPage;    //总页数
                            _this.selectedSpareList=[];   // 打开弹窗选中的清空
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }                        
                        _this.selectDisabled=false;    
                    }
                });
            },
            //新增弹窗中的确定按钮   选择备件
            selectSpare(index,row){    //选中
                var _this=this;
                for(var i=0;i<_this.planDetailsData.length;i++){
                    if(row.code==_this.planDetailsData[i].materialCode){
                        //将备件已选的记录下来
                        _this.selSpare.push(row.code);
                        return;
                    }
                }
                if(row.checked){
                    _this.selectedSpareList.push({"id":row.id,"materialSubclass":row.subclassName,"materialName":row.name,"materialCode":row.code,"standard":row.standard,"unit":row.unit});
                }else{
                    _this.selectedSpareList.pop({"id":row.id,"materialSubclass":row.subclassName,"materialName":row.name,"materialCode":row.code,"standard":row.standard,"unit":row.unit});
                }
                /*if($(event.target).parent().attr("class").indexOf("is-checked")!=-1){
                    _this.selectedSpareList.push({"id":row.id,"subclass":row.subclassName,"materialName":row.name,"materialCode":row.code,"standard":row.standard,"unit":row.unit});   //新增供应商弹框中，选中的供应商的信息
                }else{
                    _this.selectedSpareList.pop({"id":row.id,"subclass":row.subclassName,"materialName":row.name,"materialCode":row.code,"standard":row.standard,"unit":row.unit});
                }*/
            },

            insertMateraialSupplier(){   //选择备件弹窗  确定按钮  确定新增计划明细
                var _this=this;
                _this.dialogFormVisible = false;
                if(_this.selSpare.length!=0){
                    var selSpare=_this.selSpare.join(",");
                    _this.common.errorTip("备件"+selSpare+"已选")
                }
                for(var i=0;i<_this.selectedSpareList.length;i++){
                    _this.planDetailsData.push({
                        materialSubclass: _this.selectedSpareList[i].materialSubclass,
                        materialCode:_this.selectedSpareList[i].materialCode,
                        materialName:_this.selectedSpareList[i].materialName,
                        standard:_this.selectedSpareList[i].standard,
                        unit:_this.selectedSpareList[i].unit,
                        deliveryTimeExpectation:"",
                        quantity:0,
                        currencyCode:_this.createPlan.currencyCode,
                        currencyCodeWord:_this.createPlan.currencyCodeWord,
                        price:0,
                        removed:false
                    });
                }
            },
            // 输入计划数量
            planQuantity(index,row){
                var _this=this;
                row.amount=(row.price * row.quantity).toFixed(2);
                _this.createPlan.totalCount=0;
                _this.createPlan.total=0;
                setTimeout(function(){
                     for(var i=0;i<_this.planDetailsData.length;i++){
                        _this.createPlan.totalCount +=_this.planDetailsData[i].quantity;
                        _this.createPlan.total +=_this.planDetailsData[i].price * _this.planDetailsData[i].quantity;
                    }
                    if(typeof _this.createPlan.total=="number"){
                        _this.createPlan.total=_this.createPlan.total.toFixed(2);
                    }
                },0);
            },
            // 输入单价
            planPrice(index,row){
                var _this=this;
                if(row.price != "" || row.price != null){
                    // row.price = row.price.toFixed(2);
                    row.amount=(row.price * row.quantity).toFixed(2);
                    _this.createPlan.totalCount=0;
                    _this.createPlan.total=0;
                    setTimeout(function(){
                        for(var i=0;i<_this.planDetailsData.length;i++){
                            _this.createPlan.totalCount +=_this.planDetailsData[i].quantity;
                            _this.createPlan.total +=_this.planDetailsData[i].price * _this.planDetailsData[i].quantity;
                        }
                        if(typeof _this.createPlan.total=="number"){
                            _this.createPlan.total=_this.createPlan.total.toFixed(2);
                        }
                    },0);
                }else{
                    _this.common.errorTip("单价不能为空");
                }
            },
            // 选择期望到货日期 格式化
            selectedDeliveryTimeExpectation(index,row){
                var _this=this;
                row.deliveryTimeExpectation=moment(row.deliveryTimeExpectation).format("YYYY-MM-DD");
            },
            // 新增的保存
            saveNew(){
                var _this=this;
                if(_this.createPlan.planId == "" || _this.createPlan.planId == null){
                    var tmp = {legalPersonId:_this.createPlan.legalPersonId,source:_this.createPlan.source,currencyCode:_this.createPlan.currencyCode,planItems:_this.planDetailsData};
                    $.ajax({
                        type: 'POST',
                        url: _this.procurement_service+"plan/create",
                        cache: false,
                        data:JSON.stringify(tmp),
                        dataType:'JSON',
                        contentType: "application/json;charset=utf-8",     //*****
                        success: function (data) {
                            if (data.success) {
                                // _this.delFlag=false;
                                _this.common.successTip("保存成功！");
                                _this.createPlan.planId=data.data.planId;
                                _this.createPlan.planCode=data.data.planCode;
                                _this.createPlan.createTime=data.data.createTime;
                                _this.createPlan.status=data.data.status;
                                _this.planDetailsData=data.data.planItems;
                                for(var i=0;i<_this.legalPersonLists.length;i++){
                                    if(_this.createPlan.legalPersonId==_this.legalPersonLists[i].id){
                                        _this.createPlan.legalPersonName=_this.legalPersonLists[i].legalPersonName;
                                    }
                                }
                                //输入的金额保留两位
                                for(var i=0;i<_this.planDetailsData.length;i++){
                                    _this.planDetailsData[i].price=Number(_this.planDetailsData[i].price).toFixed(2);
                                    _this.planDetailsData[i].amount=Number(_this.planDetailsData[i].amount).toFixed(2);
                                }
                                _this.common.successTip("保存成功！");
                            }else
                                // console.log(data.errorMessage); 
                                _this.common.errorTip(data.errorMessage);
                        }
                    });
                }
            },
            // 修改的保存
            saveOld(){
                var _this=this;
                // if(_this.delFlag){
                //     _this.planDetailsData=_this.seedTableData;
                // }
                var tmp = {legalPersonId:_this.createPlan.legalPersonId,planId:_this.createPlan.planId,currencyCode:_this.createPlan.currencyCode,status:_this.createPlan.status,planItems:_this.planDetailsData};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"plan/update",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            // _this.delFlag=false;
                            //输入的金额保留两位
                            for(var i=0;i<_this.planDetailsData.length;i++){
                                _this.planDetailsData[i].price=Number(_this.planDetailsData[i].price).toFixed(2);
                                _this.planDetailsData[i].amount=Number(_this.planDetailsData[i].amount).toFixed(2);
                            }
                            _this.common.successTip("保存成功！");
                        }else
                            // console.log(data.errorMessage);                             
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 保存计划
            savePlan(){
                var _this=this;
                _this.$refs['createPlan'].validate((valid) => {
                    if (valid) {
                        if(_this.createPlan.planCode == "" || _this.createPlan.planCode == null){
                            _this.saveNew();
                        }else{
                            _this.saveOld();
                        }
                    } else {
                        _this.common.errorTip("存在未填写的必填项！");
                        return false;
                    }
                });
            },
             // 新增的提交 submitNew
            submitNew(){
                var _this=this;
                var tmp = {legalPersonId:_this.createPlan.legalPersonId,source:_this.createPlan.source,currencyCode:_this.createPlan.currencyCode,planItems:_this.planDetailsData};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"plan/submitNew",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.common.successTip("提交成功！");
                            _this.$router.push("/SparePlan");
                            _this.createPlan=data.data;
                            _this.planDetailsData=data.data.planItems;
                            for(var i=0;i<_this.legalPersonLists.length;i++){
                                if(_this.createPlan.legalPersonId==_this.legalPersonLists[i].id){
                                    _this.createPlan.legalPersonName=_this.legalPersonLists[i].legalPersonName;
                                }
                            }
                        }else
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 修改的提交 submitOld
            submitOld(){
                var _this=this;
                // if(_this.delFlag){
                //     _this.planDetailsData=_this.seedTableData;
                // }
                var tmp = {legalPersonId:_this.createPlan.legalPersonId,planId:_this.createPlan.planId,currencyCode:_this.createPlan.currencyCode,status:_this.createPlan.status,planItems:_this.planDetailsData};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"plan/submitOld",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.common.successTip("提交成功！");
                            _this.$router.push("/SparePlan");
                            // _this.delFlag=false;
                        }else
                            // console.log(data.errorMessage); 
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 新增计划 提交计划
            submitPlan(){
                var _this = this;
                _this.$refs['createPlan'].validate((valid) => {
                    if (valid) {
                        if(_this.createPlan.planCode == "" || _this.createPlan.planCode == null){
                            _this.submitNew();
                        }else{
                            _this.submitOld();
                        }
                    } else {
                        _this.common.errorTip("存在未填写的必填项！");
                        return false;
                    }
                });
                
                // _this.planItems=[];
                // for(var i=0;i<_this.planDetailsData.length;i++){
                //     _this.planItems.push({
                //         planItemId:_this.planDetailsData[i].planItemId,
                //         materialCode:_this.planDetailsData[i].materialCode,
                //         quantity:_this.planDetailsData[i].quantity,
                //         deliveryTimeExpectation:_this.planDetailsData[i].deliveryTimeExpectation,
                //         price:_this.planDetailsData[i].price,
                //         removed:_this.planDetailsData[i].removed,
                //         remark:_this.planDetailsData[i].remark
                //     });
                // }
                // var tmp = {planId:_this.createPlan.planId,legalPersonId:_this.createPlan.legalPersonId,planItems:_this.planItems};
                // if(_this.createPlan.source){
                //     tmp.source=_this.createPlan.source;
                // }
                // if(_this.createPlan.source){
                //     tmp.currencyCode=_this.createPlan.currencyCode;
                // }
                // $.ajax({
                //     type: 'POST',
                //     url: _this.procurement_service+"plan/commit",
                //     cache: false,
                //     data:JSON.stringify(tmp),
                //     dataType:'JSON',
                //     contentType: "application/json;charset=utf-8",     //*****
                //     success: function (data) {
                //         if (data.success) {
                //             // console.log(data.data);
                //             _this.common.successTip("提交成功！");
                //             _this.$router.push("/SparePlan");
                //         }else
                //             // console.log(data.errorMessage); 
                //             _this.common.errorTip(data.errorMessage);
                //     }
                // });
            },
            // 删除
            handleDelete(index, row) {
                // rows.splice(index,1);
                var _this=this;
                // _this.delFlag=true;
                _this.seedTableData=_this.planDetailsData;
                if(row.planItemId){
                    row.removed=true;
                    // 删除已有的计划明细----ok
                    _this.planDetailsData=[];
                    for(var i=0;i<_this.seedTableData.length;i++){
                        if(!_this.seedTableData[i].removed){
                            _this.planDetailsData.push(_this.seedTableData[i]);
                        }
                    }
                }else{
                    // 新增计划明细，但未保存---ok
                    var tmpTableData=[];
                    for(var i=0;i<_this.planDetailsData.length;i++){
                        if(_this.planDetailsData[i].materialCode!=row.materialCode){
                            tmpTableData.push(_this.planDetailsData[i]);
                        }
                    }
                    _this.planDetailsData=tmpTableData;
                }
                
            },



            // //   分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                var _this = this;
                _this.sparePageSize=val;
                _this.createPlanDetais(_this.sparePageIndex, _this.sparePageSize);
            },
            // //点击页码分页查询点击事件
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                var _this = this;
                _this.sparePageIndex=val;
                _this.createPlanDetais(_this.sparePageIndex, _this.sparePageSize);
            },
            //  // ===================================
            // successTip(p) {     //成功时的提示信息
            //     this.$notify({
            //         title: '成功',
            //         message: p,
            //         type: 'success'
            //     });
            // },
            // errorTip(p) {     //错误提示
            //     this.$notify.error({
            //         title: '错误',
            //         message: p
            //     });
            // }
        }
    }
</script>

<style>

</style>